package me.firstapp.service.weixin.product.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.product.Supplier;
import me.firstapp.repository.weixin.product.SupplierRepository;
import me.firstapp.service.weixin.product.SupplierService;

@Service
public class SupplierServiceImpl implements SupplierService {

	@Autowired
	private SupplierRepository supplierRepository;

	@Override
	public Page<Supplier> findSupplierPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long id) {
		return supplierRepository.findSupplierPage(pageNo, pageSize, searchParams, sortParam, sortType, id);
	}

	@Override
	public Supplier executeAdd(String sysName, String name, String contacts, String shopUrl, String idCard, String qq,
			String ww, String weixin, String telPhone, String mobile, String email, String address, String remark) {
		Supplier supplier = new Supplier();
		supplier.setTimeAt(new Date());
		supplier.setSysName(sysName);
		supplier.setName(name);
		supplier.setContacts(contacts);
		supplier.setShopUrl(shopUrl);
		supplier.setIdCard(idCard);
		supplier.setQq(qq);
		supplier.setWw(ww);
		supplier.setWeixin(weixin);
		supplier.setTelPhone(telPhone);
		supplier.setMobile(mobile);
		supplier.setEmail(email);
		supplier.setAddress(address);
		supplier.setRemark(remark);
		supplierRepository.save(supplier);
		return supplier;
	}

	@Override
	public Supplier executeUpdate(Long id, String sysName, String name, String contacts, String shopUrl, String idCard,
			String qq, String ww, String weixin, String telPhone, String mobile, String email, String address,
			String remark) {
		Supplier supplier = (Supplier) supplierRepository.getById(id);
		if (null == supplier) {
			throw new ServiceException("error", "供应商不存在");
		}
		if (StrUtils.isNotNULL(sysName)) {
			supplier.setSysName(sysName);
		}
		if (StrUtils.isNotNULL(name)) {
			supplier.setName(name);
		}
		if (StrUtils.isNotNULL(contacts)) {
			supplier.setContacts(contacts);
		}
		if (StrUtils.isNotNULL(shopUrl)) {
			supplier.setShopUrl(shopUrl);
		}
		if (StrUtils.isNotNULL(idCard)) {
			supplier.setIdCard(idCard);
			;
		}
		if (StrUtils.isNotNULL(qq)) {
			supplier.setQq(qq);
		}
		if (StrUtils.isNotNULL(ww)) {
			supplier.setWw(ww);
		}
		if (StrUtils.isNotNULL(weixin)) {
			supplier.setWeixin(weixin);
		}
		if (StrUtils.isNotNULL(telPhone)) {
			supplier.setTelPhone(telPhone);
		}
		if (StrUtils.isNotNULL(mobile)) {
			supplier.setMobile(mobile);
		}
		if (StrUtils.isNotNULL(email)) {
			supplier.setEmail(email);
		}
		if (StrUtils.isNotNULL(address)) {
			supplier.setAddress(address);
		}
		if (StrUtils.isNotNULL(remark)) {
			supplier.setRemark(remark);
		}
		supplierRepository.update(supplier);
		return supplier;
	}

	@Override
	public Supplier findById(Long id) {
		return (Supplier) supplierRepository.getById(id);
	}

}
